package com.wkr.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wkr.common.AppException;
import com.wkr.common.CommonResponse;
import com.wkr.order.service.ConsumerService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/train")
public class PurchaseController {

    @Autowired
    ConsumerService consumerService;

    @GlobalTransactional(name = "my_test_tx_group", rollbackFor = Exception.class)
    @SentinelResource(value = "purchase", fallback = "purchaseFallBack", exceptionsToIgnore = AppException.class)
    @GetMapping("/purchase")
    public CommonResponse<Boolean> purchase(@RequestParam boolean ex) {
        log.info("请求进入");
        consumerService.purchase(ex);
        log.info("数据返回");
        return CommonResponse.success(Boolean.TRUE);
    }

    public static CommonResponse<Boolean> purchaseFallBack() {
        return CommonResponse.fail("-1", "当前人数访问过多，请稍后重试！");
    }
}
